import { DifyChatProvider } from '@dify-chat/core'
import { initResponsiveConfig } from '@dify-chat/helpers'
import FingerPrintJS from '@fingerprintjs/fingerprintjs'
import { useMount } from 'ahooks'
import { BrowserRouter, type IRoute, Route } from 'pure-react-router'
import { useState } from 'react'

import AppListPage from './pages/app-list'
import ChatPage from './pages/chat'
import IndexPage from './pages/index'
import DifyAppService from './services/app/localstorage'

// 初始化响应式配置
initResponsiveConfig()

const routes: IRoute[] = [
	{ path: '/chat', component: () => <ChatPage /> },
	{ path: '/app/:appId', component: () => <ChatPage /> },
	{ path: '/apps', component: () => <AppListPage /> },
	{ path: '/', component: () => <IndexPage /> },
]

/**
 * Dify Chat 的最小应用实例
 */
export default function App() {
	
	const [userId, setUserId] = useState<string>('')

	useMount(() => {
		// 模拟登录过程获取用户唯一标识
		const loadFP = async () => {
			const fp = await FingerPrintJS.load()
			const result = await fp.get()
			setUserId(result.visitorId)
		}
		loadFP()
	})

	return (
		<BrowserRouter
			basename="/dify-chat"
			routes={routes}
		>
			<DifyChatProvider
				value={{
				// 修改为单应用模式
				mode: 'singleApp',
				// 用户id，可以获取业务系统的用户 ID，动态传入
				user: userId,
				// 单应用模式下，需要传入 appConfig 配置
				appConfig: {
					requestConfig: {
						apiBase: 'https://ai-lab.zicp.fun/v1',
						apiKey: 'app-g59LXq3hgQrC1PypWksadIOF',
					},
				},
			}}
			>
				<Route />
			</DifyChatProvider>
		</BrowserRouter>
	)
}
